package com.pingan.haofang.searchcloud.common.configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.pingan.haofang.searchcloud.common.vo.ExceptionVO;
import com.pingan.haofang.searchcloud.common.vo.FieldExceptionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fasterxml.classmate.TypeResolver;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger相关配置
 *
 * @author LUYI374
 * @date 2017年2月28日
 * @since 1.0.0
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    /**
     * 对外服务swagger文档分组
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {

        ModelRef errorModel = new ModelRef("ExceptionVO");

        List<ResponseMessage> responseMessages = Arrays.asList(
                new ResponseMessageBuilder().code(409).message("Conflict")
                        .responseModel(new ModelRef("FieldExceptionVO")).build(),
                new ResponseMessageBuilder().code(403).message("Forbidden").responseModel(null).build(),
                new ResponseMessageBuilder().code(404).message("Not Found").responseModel(errorModel).build(),
                new ResponseMessageBuilder().code(500).message("Internal Error").responseModel(null).build());

        // 可以添加多个header或参数
        ParameterBuilder appIdBuilder = new ParameterBuilder();
        appIdBuilder.name("appCode").description("app唯一标识,对应项目编码").modelRef(new ModelRef("string"))
                .parameterType("header").required(true).build();

        ParameterBuilder tokenBuilder = new ParameterBuilder();
        tokenBuilder.name("token").description("访问token,请联系管理员获取").modelRef(new ModelRef("string"))
                .parameterType("header").required(true).build();

        ParameterBuilder traceIdBuilder = new ParameterBuilder();
        traceIdBuilder.name("traceId").description("分布式日志ID，请随机生成字符串传参,如:T-hft-2017043012304534,否则平台无法进行排查")
                .modelRef(new ModelRef("string"))
                .parameterType("header").required(true).build();

        List<Parameter> headerParameter = new ArrayList<Parameter>();
        headerParameter.add(appIdBuilder.build());
        headerParameter.add(tokenBuilder.build());
        headerParameter.add(traceIdBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.pingan.haofang.searchcloud.api")).paths(PathSelectors.any())
                .build().groupName("外部接口")
                .globalOperationParameters(headerParameter).apiInfo(apiInfo()).useDefaultResponseMessages(false)
                .additionalModels(typeResolver.resolve(FieldExceptionVO.class), typeResolver.resolve(ExceptionVO.class))
                .globalResponseMessage(RequestMethod.POST, responseMessages)
                .globalResponseMessage(RequestMethod.PUT, responseMessages)
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .globalResponseMessage(RequestMethod.DELETE, responseMessages);

    }

    /**
     * 内部管理平台接口分组
     *
     * @return
     */
    @Bean
    public Docket createWebApi() {

        ModelRef errorModel = new ModelRef("ExceptionVO");

        List<ResponseMessage> responseMessages = Arrays.asList(
                new ResponseMessageBuilder().code(409).message("Conflict")
                        .responseModel(new ModelRef("FieldExceptionVO")).build(),
                new ResponseMessageBuilder().code(401).message("Unauthorized").responseModel(null).build(),
                new ResponseMessageBuilder().code(403).message("Forbidden").responseModel(null).build(),
                new ResponseMessageBuilder().code(404).message("Not Found").responseModel(errorModel).build(),
                new ResponseMessageBuilder().code(500).message("Internal Error").responseModel(errorModel).build());

        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.pingan.haofang.searchcloud.web"))
                .paths(PathSelectors.any())
                .build().groupName("管理端接口")
                .apiInfo(apiInfo()).useDefaultResponseMessages(false)
                .additionalModels(typeResolver.resolve(FieldExceptionVO.class), typeResolver.resolve(ExceptionVO.class))
                .globalResponseMessage(RequestMethod.POST, responseMessages)
                .globalResponseMessage(RequestMethod.PUT, responseMessages)
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .globalResponseMessage(RequestMethod.DELETE, responseMessages);

    }
    
    
    /**
     * 用户服务接口分组
     * @return
     */
    @Bean
    public Docket createAccountApi() {
        List<ResponseMessage> responseMessages = Arrays.asList(
                new ResponseMessageBuilder().code(409).message("Conflict")
                        .responseModel(new ModelRef("FieldError")).build(),
                new ResponseMessageBuilder().code(403).message("Forbiddon").responseModel(null).build(),
                new ResponseMessageBuilder().code(404).message("Not Found").responseModel(null).build(),
                new ResponseMessageBuilder().code(500).message("Intenal Error").responseModel(null).build());

        List<Parameter> headerParameter = new ArrayList<Parameter>();

        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.pingan.haofang.gov.sm.account")).paths(PathSelectors.any())
                .build().groupName("用户服务接口")
                .globalOperationParameters(headerParameter).apiInfo(accountApiInfo()).useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.POST, responseMessages)
                .globalResponseMessage(RequestMethod.PUT, responseMessages)
                .globalResponseMessage(RequestMethod.GET, responseMessages)
                .globalResponseMessage(RequestMethod.DELETE, responseMessages);

    }

    @Autowired
    private TypeResolver typeResolver;

    private ApiInfo accountApiInfo() {
        Contact contact = new Contact("建管云通用服务2组", "", "JIANGKUI450@pingan.com.cn");
        return new ApiInfoBuilder().title("Gov Cloud account API  DOC").contact(contact).description("模块化account").build();
    }
    
    private ApiInfo apiInfo() {
        Contact contact = new Contact("用户产品研发部四组", "", "huhailiang937@pingan.com.cn");
        return new ApiInfoBuilder().title("Data Search API  DOC").contact(contact).description("平安好房搜索云服务").build();

    }
}
